package com.navi.core.service.impl;

import com.navi.SpringContext;
import com.navi.core.service.ICIMService;
import com.navi.core.service.ISendMessageService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Properties;


@Service
public class SendMessageServiceImpl implements ISendMessageService {
    @Autowired
    private static Properties props;

    @PostConstruct
    public void init(){
        try {
            props = new Properties();
            props.load(SendMessageServiceImpl.class.getResourceAsStream("/triger.properties"));
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.toString());
        }
    }

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String sendMessage(String trxId, String strInObj) {
        //logger.info("trxId: {} , strInObj: {} ",trxId,strInObj);
        String serviceName = getBeanNameByTrxID(trxId);
        //logger.info("trxId: {} , strInObj: {} , serviceName:{} ",trxId,strInObj,serviceName);
        if(StringUtils.isEmpty(serviceName)){
            logger.error(strInObj);
        }
        ICIMService icimService = (ICIMService) SpringContext.getBean(serviceName);
        return icimService.subMainProc(strInObj);
    }

    private String getBeanNameByTrxID(String trx_id) {
        if ("XPREPORT".equals(trx_id)) {
            return "reportServiceImpl";
        }
        props = new Properties();
        try {
            props.load(SendMessageServiceImpl.class.getResourceAsStream("/triger.properties"));
        } catch (IOException e) {
            logger.error("Read trigger configuration error");
            e.printStackTrace();
            logger.error(e.toString());
        }
        String beanName = props.getProperty(trx_id);
        return beanName;
    }
}
